<?php

namespace App\Helper;



/**
 * 身份证实名
 */
class IdcardSDK
{

    protected $host = "https://eid.shumaidata.com";
    protected $path = "/eid/check";
    protected $method = "POST";
    protected $appcode = "4f46a91e28a0493e8da28230d044b2a3";
    protected $headers = array();
    protected $idcard="";
    protected $truename="";
    public function __construct($idcard="",$truename=""){
        $this->idcard=$idcard;
        $this->truename=$truename;
    }

    /**
     * 认证检查
     * @return bool|string
     */
    public function check(){
        array_push($this->headers, "Authorization:APPCODE " . $this->appcode);
        $querys = "idcard=".$this->idcard."&name=".urlencode($this->truename);
        $bodys = "";
        $url = $this->host . $this->path . "?" . $querys;
        $response=$this->_post($url,$this->method,$this->headers,$this->host);
        if($response->code==0){
            return true;
        }
        return false;
    }

    protected function _post($url="",$method="",$headers=[],$host=""){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        //设定返回信息中是否包含响应信息头，启用时会将头文件的信息作为数据流输出，true 表示输出信息头, false表示不输出信息头
        //如果需要将字符串转成json，请将 CURLOPT_HEADER 设置成 false
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$".$host, "https://"))
        {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $result=curl_exec($curl);
        return json_decode($result);
    }






}
